﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using DTO;
namespace GUI
{
    public partial class uc_KhachHang : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_KhachHang()
        {
            InitializeComponent();
        }
        CustomerBL customerBL = new CustomerBL();
        CustomerDTO cus = new CustomerDTO();
        private void textEdit1_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void txtMaKH_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
            if ((Keys)e.KeyChar == Keys.Enter)
            {
                cus = customerBL.Custummer_SelectByID(int.Parse(txtMaKH.Text));
                txtTenKH.Text = cus.Name;
                txtDiaChiKH.Text = cus.Address;
                txtSDT.Text = cus.Phone;
            }
        }

        private void uc_KhachHang_Load(object sender, EventArgs e)
        {
            customerTableAdapter.Fill(sellingmobilemanagementDataSet1.customer);
            txtMaKH.Enabled = true;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Cập nhật khách hàng?", "Confirmation", MessageBoxButtons.YesNo) ==
               DialogResult.Yes)
           {
               cus.Address = txtDiaChiKH.Text;
               cus.Name = txtTenKH.Text;
               cus.Phone = txtSDT.Text;
                if (radioGroup1.SelectedIndex == 0)
                    customerBL.Customer_Update(cus);
                else if (radioGroup1.SelectedIndex == 1)
                    customerBL.Customer_Insert(cus);
                MessageBox.Show("Cập Nhật Khách Hàng Thành Công", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                customerTableAdapter.Fill(sellingmobilemanagementDataSet1.customer);
           }
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            txtMaKH.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "CustomerID").ToString();
            txtTenKH.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Name").ToString();
            txtDiaChiKH.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Address").ToString();
            txtSDT.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Phone").ToString();
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioGroup1.SelectedIndex == 1)
            {
                txtMaKH.Enabled = false;
                txtDiaChiKH.Text = "";
                txtSDT.Text = "";
                txtTenKH.Text = "";
            }
            else txtMaKH.Enabled = true;
        }
    }
}
